//
//  LGCiTangCreateFamilyTreeController.m
//  ligenyun
//
//  Created by Mac on 2018/3/26.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGCiTangCreateFamilyTreeController.h"
#import "STRadioButton.h"
@interface LGCiTangCreateFamilyTreeController ()
@property(nonatomic, strong) STButton                     *addButton;
@property(nonatomic, strong) UIImage                     *originAddImage;
@property(nonatomic, strong) NSArray                     *dataSouce;

@property(nonatomic, strong) STRadioButton                 *radioButton;
@property(nonatomic, strong) UITextField                  *moneyTextFiled;
@property(nonatomic, strong) STButton                     *shenheButton;
@property(nonatomic, strong) STButton                     *shareButton;
@property(nonatomic, strong) LGTypeChosedControl                     *typeChosedControl;
@end

@implementation LGCiTangCreateFamilyTreeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"族谱";
    self.originAddImage = [UIImage imageNamed:@""];
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    [self configtTableHeader];
    [self configtTableFooter];
    [self configDataSouce];
    [self configBottomView];
}

- (void)configtTableHeader{
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 100)];
    header.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 10, 100 , 75)
                                                        title:@""
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:[UIColor whiteColor]
                                              backgroundImage:nil
                                                        image:[UIImage imageNamed:@"icon_添加图片"]];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedAddImageButton];
    }];
    confimButton.centerX = UIScreenWidth / 2;
    confimButton.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0 );
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:confimButton];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, confimButton.width, 20)
                                                     text:@"点击添加图片"
                                                textColor:ThirdTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.bottom = confimButton.height - 5;
    [confimButton addSubview:titleLable];
    header.height = confimButton.bottom + 10;
    self.addButton = confimButton;
    [confimButton st_setBorderWith:1 borderColor:TM_lineColor cornerRadius:0];
    header.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = header;
}
- (void)configtTableFooter{
    __weak typeof(self) weakSelf =  self;
    [self.tableView st_autoAdjustAllResponder];
    
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = [UIColor whiteColor];

    STLabel * quxianLable = [self deflutLableWithTitle:@"权限:"];
    [footer addSubview:quxianLable];
    
    STRadioButton * radio = [[STRadioButton alloc] initWithRadio:@[@"族谱卡可见",@"打赏可见",@"VIP可见",@"全部可见"]
                                                     andItemSize:CGSizeMake(100, 30)
                                                    andWithFrame:CGRectMake(quxianLable.right  + 30, 10, 220, 60)];
    [footer addSubview:radio];
    self.radioButton = radio;
    self.radioButton.makenumOfarrayChecked = 0 ;
    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, radio.bottom + 22, UIScreenWidth, 0.5)];
    line1.backgroundColor = TM_lineColor;
    [footer addSubview:line1];
    
    
    STLabel * moneyLable = [self deflutLableWithTitle:@"打赏最低金额:"];
    moneyLable.top = line1.bottom;
    [footer addSubview:moneyLable];
    
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(moneyLable.right, moneyLable.top, 200, 44)];
    textFiled.keyboardType = UIKeyboardTypeNumberPad;
    textFiled.textAlignment = NSTextAlignmentRight;
    textFiled.placeholder = @"请输入金额";
    self.moneyTextFiled = textFiled;
    [footer addSubview:textFiled];
    
    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(0, textFiled.bottom , UIScreenWidth, 0.5)];
    line2.backgroundColor = TM_lineColor;
    [footer addSubview:line2];
    
    STLabel * sheheLable = [self deflutLableWithTitle:@"打赏审核:"];
    sheheLable.top = line2.bottom;
    [footer addSubview:sheheLable];
    
    STButton * shenheButton = [[STButton alloc] initWithFrame:CGRectMake(sheheLable.right + 30, sheheLable.top, UIScreenWidth - 40, 44)
                                                   title:@"需要审核"
                                              titleColor:SecendTextColor
                                               titleFont:15
                                            cornerRadius:0
                                         backgroundColor:nil
                                         backgroundImage:nil
                                                   image:[UIImage imageNamed:@"icon_勾选_未选中"]];
    shenheButton.titleEdgeInsets= UIEdgeInsetsMake(0, 10, 0, 0);
    [shenheButton setImage:[UIImage imageNamed:@"icon_勾选_选中"] forState:UIControlStateSelected];
    [shenheButton setClicAction:^(UIButton *sender) {
        sender.selected = !sender.selected;
    }];
    self.shenheButton = shenheButton;
    [footer addSubview:shenheButton];
    
    UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, shenheButton.bottom , UIScreenWidth, 0.5)];
    line3.backgroundColor = TM_lineColor;
    [footer addSubview:line3];
    
    
    STLabel * fenxiangLable = [self deflutLableWithTitle:@"分享提成:"];
 
    fenxiangLable.top = line3.bottom;
    [footer addSubview:fenxiangLable];
    
    STButton * fenxiangButton = [[STButton alloc] initWithFrame:CGRectMake(sheheLable.right + 30, fenxiangLable.top, UIScreenWidth - 40, 44)
                                                        title:@"允许分享提成"
                                                   titleColor:SecendTextColor
                                                    titleFont:15
                                                 cornerRadius:0
                                              backgroundColor:nil
                                              backgroundImage:nil
                                                        image:[UIImage imageNamed:@"icon_勾选_未选中"]];
    fenxiangButton.titleEdgeInsets= UIEdgeInsetsMake(0, 10, 0, 0);
    [fenxiangButton setImage:[UIImage imageNamed:@"icon_勾选_选中"] forState:UIControlStateSelected];
    [fenxiangButton setClicAction:^(UIButton *sender) {
        sender.selected = !sender.selected;
    }];
    self.shareButton = fenxiangButton;
    [footer addSubview:fenxiangButton];
    
    UIView * line4 = [[UIView alloc] initWithFrame:CGRectMake(0, fenxiangLable.bottom , UIScreenWidth, 0.5)];
    line4.backgroundColor = TM_lineColor;
    [footer addSubview:line4];
    
    
    STLabel * timeLable = [self deflutLableWithTitle:@"打赏后查看时效:"];
    timeLable.top = line4.bottom;
    [footer addSubview:timeLable];
    
    
    LGTypeChosedControl * control = [[LGTypeChosedControl alloc] initWithFrame:CGRectMake(timeLable.right + 10, timeLable.top, 200, 30) title:@"1天"];
    control.centerY = timeLable.centerY;
    self.typeChosedControl = control;
    [control setClction:^(LGTypeChosedControl *control) {
        [weakSelf onSelctedTimeTypeButton];
    }];
    [footer addSubview:control];
    
    UIView * line5 = [[UIView alloc] initWithFrame:CGRectMake(0, timeLable.bottom , UIScreenWidth, 0.5)];
    line5.backgroundColor = TM_lineColor;
    [footer addSubview:line5];
    
    
    UIView * grawView = [[UIView alloc] initWithFrame:CGRectMake(0, line5.bottom, UIScreenWidth, 20 + 49)];
    grawView.backgroundColor = self.tableView.backgroundColor;
    [footer addSubview:grawView];
    footer.height = grawView.bottom;
    self.tableView.tableFooterView = footer;
}
- (STLabel*)deflutLableWithTitle:(NSString*)title{
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, 200, 44)
                                                     text:title
                                                textColor:SecendTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    titleLable.textColor = UIColorFromRGBA(0x333333);
    titleLable.font = [UIFont systemFontOfSize:14];
    
    [NSString lableAutoAdjustWitdhWithLabel:titleLable];
    return titleLable;
}
- (void)configDataSouce{
    
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"谱名" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"起始姓氏" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"简介" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"始祖名" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"房系" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"券号" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"修谱时间" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"当今地址" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"原籍地址" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"联系人地址" detailString:@""],
                       ];
    [self.tableView reloadData];
}
- (void)configBottomView{
    __weak typeof(self) weakSelf =  self;
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 2, 49)
                                                     title:@"提交"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:18
                                              cornerRadius:0
                                           backgroundColor:TM_redColor
                                           backgroundImage:nil
                                                     image:nil];
    [buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCommitButton];
    }];
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    buyButton.top = [TMUtils tabBarTop];
    [self.view addSubview:buyButton];
    
    STButton *resetButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 2, 49)
                                                     title:@"重置"
                                                titleColor:FirstTextColor
                                                 titleFont:18
                                              cornerRadius:0
                                           backgroundColor:FlatGray
                                           backgroundImage:nil
                                                     image:nil];
    [resetButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedResetButton];
    }];
    resetButton.left = UIScreenWidth /2 ;
    [self.view addSubview:resetButton];
    resetButton.top = [TMUtils tabBarTop];
    resetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSouce[indexPath.section];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     STTableEasyModel * model = self.dataSouce[indexPath.section];
    if ([model.textString containsString:@"当今地址"] || [model.textString containsString:@"原籍地址"] ) {
        [self onSelcedAreaWithIndexPath:indexPath model:model];
    }else if ([model.textString containsString:@"修谱时间"]  ) {
        [self onSelceddateWithIndexPath:indexPath model:model];
    }else{
        NSString * placeHolder = [NSString stringWithFormat:@"请输入%@",model.textString];
        STTableViewDetailController * detail =
        [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                           title:model.textString
                                                            text:model.detailString
                                                          handle:^(NSString *text) {
                                                              if (text.length && ![text isEqualToString:model.detailString]) {
                                                                  model.detailString = text;
                                                                  [self.tableView reloadData];
                                                              }
                                                              if (!text.length) {
                                                                  
                                                                  [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@不能为空",model.textString]];
                                                              }
                                                              [self.navigationController popViewControllerAnimated:YES];
                                                          }];
        detail.detailLabel.text = @"";
        detail.isShowTextNum = NO;
        [self.navigationController pushViewController:detail animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelctedAddImageButton{
    [self showDefultImagePicker:^(UIImage *image) {
        [self.addButton setImage:image forState:UIControlStateNormal];
        for (UIView * view in self.addButton.subviews) {
            [view removeFromSuperview];
        }
    }];
}
//时间
- (void)onSelceddateWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtDatePickerWithHandle:^(NSDate *date) {
        model.detailString = date.st_yyyyMMddByLineWithDate;
        [self.tableView reloadData];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
//地址和原籍地址
- (void)onSelcedAreaWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtAreaPickerWithHandle:^(NSString *finshString, NSString *area, NSString *city, NSString *provence) {
        model.detailString = [NSString stringWithFormat:@"%@%@",provence,city];
        [self.tableView reloadData];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
- (void)onSelctedCommitButton{
    for (STTableEasyModel * mdoel in self.dataSouce) {
        if (!mdoel.detailString.length) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@不能为空",mdoel.textString]];
            return;
        }
    }
    if (!self.moneyTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入打赏最低金额"];
        return;
    }
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)onSelctedResetButton{
    [self showAlertCancelAndConfirm:@"重置后,填写的资料会消失，确认重置吗？" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
             [self configSubView];
        }
    }];
   
}
- (void)onSelctedTimeTypeButton{
    NSArray * array = @[@"1天",@"10天",@"30天",@"180天",@"365天"];
    STPickerViewController * picker = [[STPickerViewController alloc] initWithPickerArray:@[array] andWithHandle:^(NSArray<NSString *> *stringArray) {
       self.typeChosedControl.title = stringArray.lastObject;

    }];
    [self presentViewController:picker animated:NO completion:nil];
}

@end
